package vn.gq.tym.tonghopvf;

import vn.gq.tym.utils.DialogHelper;
import vn.gq.tym.utils.GooglePlay;
import vn.gq.tym.utils.IDialogHelper;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.apperhand.device.android.AndroidSDKProvider;

public class HomeActivity extends BaseActivity implements OnClickListener {

	private Button btnReading, btnContact, btnGoPlay, btnExit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AndroidSDKProvider.initSDK(this);
		setContentView(R.layout.activity_home);
		if (!getString(R.string.author).equals("")) {
			getSupportActionBar().setSubtitle(getString(R.string.author));
		}
		btnReading = (Button) findViewById(R.id.btnRead);
		btnReading.setOnClickListener(this);
		btnGoPlay = (Button) findViewById(R.id.btnStore);
		btnGoPlay.setOnClickListener(this);
		btnContact = (Button) findViewById(R.id.btnContact);
		btnContact.setOnClickListener(this);
		btnExit = (Button) findViewById(R.id.btnExit);
		btnExit.setOnClickListener(this);
	}
	
	
	public void sendEmail() {
		PackageInfo pkgInfo = null;
		try {
			pkgInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		String mEmailBody = String.format("\n\n\n\n\n\n" +
				"-------------------" +
				"\nTên ứng dụng: %s\nMã ứng dụng: %s", getString(R.string.app_name), getPackageName());
		if (pkgInfo != null) {
			mEmailBody += String.format("\nMã phiên bản: %d", pkgInfo.versionCode);
		}
		mEmailBody += "\n-------------------";
		
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.putExtra(Intent.EXTRA_SUBJECT, "Góp ý, nhận xét, yêu cầu truyện");
		intent.putExtra(Intent.EXTRA_TEXT, mEmailBody);
		intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "ungdungviet.llc@gmail.com" });
		intent.setType("message/rfc822");
		try {
			startActivity(Intent.createChooser(intent, "Chọn ứng dụng gửi mail"));
		} catch(ActivityNotFoundException e) {
			Toast.makeText(this, "Không tìm thấy ứng dụng email trong máy", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(btnReading)) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
		} else if (v.equals(btnGoPlay)) {
			GooglePlay.choosePublisherPlay(this);
		} else if (v.equals(btnContact)) {
			sendEmail();
		} else if (v.equals(btnExit)) {
			showDialogOnExit();
		}
	}
	
	public void showDialogOnExit() {
		DialogHelper.showExitDialog(this, "Hãy dành chút thời gian đánh giá cho ứng dụng!", new IDialogHelper() {
			
			@Override
			public void onOk() {
				GooglePlay.startAcitivityForDetail(HomeActivity.this);
			}
			
			@Override
			public void onCancel() {
				onDestroy();
				System.runFinalizersOnExit(true);
				System.exit(0);
			}
		});
	}

	@Override
	public void onBackPressed() {
		showDialogOnExit();
	}
	
	

}
